package com.lyfei.ydc.domain.entity;

import com.baomidou.mybatisplus.annotation.TableField;
import com.baomidou.mybatisplus.annotation.TableLogic;
import com.baomidou.mybatisplus.annotation.TableName;
import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;
import com.lyfei.ydc.domain.BaseEntity;
import lombok.*;

import javax.persistence.Transient;
import java.lang.reflect.Type;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;

@NoArgsConstructor
@AllArgsConstructor
@Getter
@Setter
@Builder
@TableName("t_user")
public class User extends BaseEntity<User> {
    private String openid;

    private String nickname;

    private String avatarUrl;

    private String email;

    private String mobile;

    private String password;

    private Long unifyUid;

    private Date createTime;

    private Date updateTime;

    @TableLogic
    private Boolean deleted;

    @Transient
    private String wxProfile;

    @TableField(exist = false)
    private Map<String, Object> wxProfileMap;

    public Map<String, Object> getWxProfileMap() {
        if(this.wxProfile != null) {
            Gson gson = new Gson();

            Type type = new TypeToken<Map<String, Object>>(){}.getType();

            // 将 JSON 字符串解析为原始的 Map
            return gson.fromJson(this.wxProfile, type);
        }

        return new HashMap<>();
    }
}
